﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormRankMarcaModelo : Form
    {
        public FormRankMarcaModelo()
        {
            InitializeComponent();
        }
        private void FormRankMarcaModelo_Load(object sender, EventArgs e)
        {
            this.dateTimePickerFim.Value = DateTime.Today;
        }

        private void buttonExecutar_Click(object sender, EventArgs e)
        {
            DateTime? dataIni = null;
            DateTime? dataFim = null;
            
            this.dataGridViewRanking.Rows.Clear();
            
            ArrayList lista = ModelBO.getRankingMarcas(this.dateTimePickerIni.Value, this.dateTimePickerFim.Value);
            int totalGeral = 0;
            foreach (object[] item in lista)
            {
                totalGeral += (int)item[2];
            }

            int totalParcial = 0;
            decimal rank = 1;
            foreach (object[] item in lista)
            {
                int nRow = this.dataGridViewRanking.Rows.Add();
                this.dataGridViewRanking.Rows[nRow].Cells["ranking"].Style.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.dataGridViewRanking.Rows[nRow].Cells["ocorrencias"].Style.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                this.dataGridViewRanking.Rows[nRow].Cells["idMarca"].Value = item[0];
                this.dataGridViewRanking.Rows[nRow].Cells["marca"].Value = item[1];
                this.dataGridViewRanking.Rows[nRow].Cells["ocorrencias"].Value = item[2];

                totalParcial += (int)item[2];
                //if ((totalParcial / totalGeral) < 0.2)
                //    this.dataGridViewRanking.Rows[nRow].Cells["ranking"].Value = 1;
                //else
                //    this.dataGridViewRanking.Rows[nRow].Cells["ranking"].Value = 2;

                this.dataGridViewRanking.Rows[nRow].Cells["ranking"].Value = (int)rank;
                rank += 0.20M;


                ArrayList listaModelo = ModelBO.getRankingModelos((int)item[0], dataIni, dataFim);

                decimal rankmodelo = 1;
                foreach (object[] itemModelo in listaModelo)
                {
                    int nRowModelo = this.dataGridViewRanking.Rows.Add();
                    this.dataGridViewRanking.Rows[nRowModelo].Cells["idModelo"].Value = itemModelo[0];
                    this.dataGridViewRanking.Rows[nRowModelo].Cells["modelo"].Value = itemModelo[1];
                    this.dataGridViewRanking.Rows[nRowModelo].Cells["ocorrencias"].Value = itemModelo[2];

                    this.dataGridViewRanking.Rows[nRowModelo].Cells["ranking"].Value = (int)rankmodelo;
                    rankmodelo += 0.20M;
                }

            }
            this.buttonAplicar.Enabled = true;

        }

        private void buttonAplicar_Click(object sender, EventArgs e)
        {
            this.buttonAplicar.Enabled = false;

            List<MarcaVO> listMarcaVOs = ModelBO.getMarcas();
            List<ModeloVO> listModeloVOs = ModelBO.getModelos();

            this.progressBar1.Maximum = listMarcaVOs.Count + listModeloVOs.Count + dataGridViewRanking.Rows.Count;
            this.progressBar1.Value = 0;
            this.progressBar1.Visible = true;

            foreach (MarcaVO marcaVO in listMarcaVOs)
            {
                this.progressBar1.Value++;
                marcaVO.ordem = 999;
                ModelBO.setMarca(marcaVO);
            }

            foreach (ModeloVO modeloVO in listModeloVOs)
            {
                this.progressBar1.Value++;
                modeloVO.ordem = 999;
                ModelBO.setModelo(modeloVO);
            }

            foreach (DataGridViewRow dgvr in dataGridViewRanking.Rows)
            {
                this.progressBar1.Value++;
                int? idMarca = (int?)dgvr.Cells["idMarca"].Value;
                if (idMarca != null)
                {
                    MarcaVO marcaVO = ModelBO.getMarca((int)idMarca);
                    marcaVO.ordem = (int)dgvr.Cells["ranking"].Value;
                    ModelBO.setMarca(marcaVO);
                }
                else
                {
                    int? idModelo = (int?)dgvr.Cells["idModelo"].Value;
                    ModeloVO modeloVO = ModelBO.getModelo((int)idModelo);
                    modeloVO.ordem = (int)dgvr.Cells["ranking"].Value;
                    ModelBO.setModelo(modeloVO);
                }
            }
            MessageBox.Show("Ranking aplicado!", "Aviso");
            this.progressBar1.Visible = false;
            this.buttonAplicar.Enabled = true;
        }

    }
}
